package com.taotao.rest.service.Impl;

import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.rest.dao.JedisClient;
import com.taotao.rest.pojo.CatNode;
import com.taotao.rest.pojo.CatResult;
import com.taotao.rest.service.ItemCatService;
import com.taotao.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by new on 2018/8/15.
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Autowired
    private JedisClient jedisClient;

    @Value("${INDEX_CATEGORY_REDIS_KEY}")
    private String INDEX_CATEGORY_REDIS_KEY;
    @Value("${INDEX_CATEGORY_KEY}")
    private String INDEX_CATEGORY_KEY;

    @Override
    public CatResult getItemCatList() {
        CatResult catResult = new CatResult();

        //从缓存中取内容
        try {
            String result = jedisClient.hget(INDEX_CATEGORY_REDIS_KEY, INDEX_CATEGORY_KEY);
            if (!StringUtils.isBlank(result)) {
                List<?> list = JsonUtils.jsonToList(result, Object.class);
                //把字符串转换成list
                catResult.setData(list);
                return catResult;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        catResult.setData(getCatList((long) 0));

        //向缓存中添加内容
        String json = null;
        try {
            //把list转换成字符串
            json = JsonUtils.objectToJson(getCatList((long) 0));
            jedisClient.hset(INDEX_CATEGORY_REDIS_KEY,INDEX_CATEGORY_KEY,json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return catResult;
    }

    /**
     * 查询分类列表
     * @param parentId
     * @return
     */
    private List<?> getCatList(Long parentId){
        //创建查询条件
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        //执行查询
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(example);
        //返回list
        List resultList = new ArrayList<>();
        //向list中添加节点
        for (TbItemCat tbItemCat : tbItemCats) {
            //如果是父节点
            if(tbItemCat.getIsParent()){
                CatNode catNode = new CatNode();
                if(parentId == 0){
                    catNode.setName("<a href='/products/"+tbItemCat.getId()+".html'>"+tbItemCat.getName()+"</a>");
                }else{
                    catNode.setName(tbItemCat.getName());
                }
                catNode.setUrl("/products/"+tbItemCat.getId()+".html");
                catNode.setItem(getCatList(tbItemCat.getId()));
                resultList.add(catNode);
            }else{
                //如果是叶子节点
                resultList.add("/products/"+tbItemCat.getId()+".html|"+tbItemCat.getName());
            }
        }
        return resultList;
    }

}
